<template>
    <div class="search-form">

        <!-- 头部tab切换 -->
        <el-row type="flex" class="search-tab">
            <span v-for="(item, index) in tabs" :key="index"
            @click="handleSearchTab(item, index)"
            :class="{active: index === currentTab}">
                <i :class="item.icon"></i>{{item.name}}
            </span>
        </el-row>

        <el-form class="search-form-content" ref="form" label-width="80px">
            <el-form-item label="出发城市">
                <!-- fetch-suggestions 返回输入建议的方法 -->
                <!-- select 点击选中建议项时触发 -->
                <el-autocomplete
                v-model="departcity"
                :fetch-suggestions="queryDepartSearch"
                placeholder="请搜索出发城市"
                @select="handleDepartSelect"
                class="el-autocomplete"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item label="到达城市">
                <el-autocomplete
                v-model="destcity"
                :fetch-suggestions="queryDestSearch"
                placeholder="请搜索到达城市"
                @select="handleDestSelect"
                class="el-autocomplete"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item label="出发时间">
                <!-- chang-e 用户确认选择日期时触发 -->
                <el-date-picker type="date" 
                v-model="date1"
                placeholder="请选择日期" 
                style="width: 100%;"
                value-format="yyyy-MM-dd"
                @change="handleDate">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="">
                <el-button style="width:100%;" 
                type="primary" 
                icon="el-icon-search"
                @click="handleSubmit">
                    搜索
                </el-button>
            </el-form-item>
            <div class="reverse">
                <span @click="handleReverse">换</span>
            </div>
        </el-form>
        <!-- <div class="test">
            测试伪对象
        </div>   -->
      </div>
</template>

<script>
export default {
    data(){
        return {
            date1:'',
            departcity:'',
            destcity:'',
            departcode:'',
            destcode:'',
            tabs: [
                {icon: "iconfont icondancheng", name: "单程"},
                {icon: "iconfont iconshuangxiang", name: "往返"}
            ],
            currentTab: 0,
        }
    },
    methods: {
        // tab切换时触发
        handleSearchTab(item, index){
            if(item.name=="往返"){
                this.currentTab=1,
                this.$confirm("目前暂未开启往返功能!","提示",{
                    confirmButtonText:"确认",
                    showCancelButton:false,
                    type:'warning'

                })
                this.currentTab=0
            }
        },
        
        // 出发城市输入框获得焦点时触发
        // value 是选中的值，cb是回调函数，接收要展示的列表
        queryDepartSearch(value, cb){
            if(value==""){
              cb([])
            }else{
              this.$axios({
                  url:"/airs/city",
                  method:"get",
                  params:{
                      name:value
                  }
              }).then(res=>{
                  console.log("96",res)
                  let arr=res.data.data
                  //此处要声明对象数组，才可以在后面动态创建属性value
                  let citys=[]
                  for(let i=0;i<arr.length;i++){
                      let city={value:arr[i].name.replace("市",""),code:arr[i].sort}
                      citys[i]=city
                  }
                  console.log(citys)
                  if(citys.length==0){
                    let city={value:"无该城市信息"}
                    citys[0]=city
                  }
                  cb(citys)
              })
            }
        },

        // 目标城市输入框获得焦点时触发
        // value 是选中的值，cb是回调函数，接收要展示的列表
        queryDestSearch(value, cb){
          if(value==''){
            cb([])
          }else{
            this.queryDepartSearch(value,cb)
          }
        },
       
        // 出发城市下拉选择时触发
        handleDepartSelect(item) {
            this.departcode=item.code
        },

        // 目标城市下拉选择时触发
        handleDestSelect(item) {
            this.destcode=item.code
        },

        // 确认选择日期时触发
        handleDate(value){
           
        },

        // 出发和目标城市切换时触发
        handleReverse(){
            let tmpCity=this.departcity
            let tmpCode=this.departcode
            this.departcity=this.destcity
            this.departcode=this.destcity
            this.destcity=tmpCity
            this.departcode=tmpCode
        },

        // 提交表单是触发
        handleSubmit(){
           //校验
           if(this.departcode==""){
            return this.$message({
              message:"请选择出发城市",
              type:"warning"
            })}
            if(this.destcode==""){
              return  this.$message({
                message:"请选择到达城市",
                type:"warning"
              })
            }
            if(this.date1==""){
              return this.$message({
                message:"请选择日期",
                type:'warning'
              })
            }
           //跳转页面并传递参数
           this.$router.push({
              path:'/air/flights',
              query:{
                departCity: this.departcity,
                departCode: this.departcode,
                destCity: this.destcity,
                destCode: this.destcode,
                departDate: this.date1
              }
            })
        }
    },
    mounted() {
       
    }
}
</script>

<style scoped lang="less">

// .test::after{
//     content:"我是伪对象的结尾"
// }
// .test::before{
//     content:"我是伪对象的开头"
// }
.search-form{
    border:1px #ddd solid;
    border-top:none;
    width:360px;
    height:350px;
    box-sizing: border-box;
}

.search-tab{
  span{
    display: block;
    flex:1;
    text-align: center;
    height:48px;
    line-height: 42px;
    box-sizing: border-box;
    border-top:3px #eee solid;
    background:#eee;
  }

  .active{
    border-top-color: orange;
    background:#fff;
  }

  i{
    margin-right:5px;
    font-size: 18px;

    &:first-child{
      font-size:16px;
    }
  }
}

.search-form-content{
  padding:15px 50px 15px 15px;
  position: relative;

  .el-autocomplete{
    width: 100%;
  }
}

.reverse{
  position:absolute;
  top: 35px;
  right:15px;

  &:after,&:before{
      display: block;
      content: "";
      position: absolute;
      left:-35px;
      width:25px;
      height:1px;
      background:#ccc;
  }

  &:after{
      top:0;
    }

    &:before{
      top:60px;
    }

  span{
    display: block;
    position:absolute;
    top: 20px;
    right:0;
    font-size:12px;
    background: #999;
    color:#fff;
    width:20px;
    height:20px;
    line-height: 18px;
    text-align: center;
    border-radius: 2px;
    cursor: pointer;

    &:after,&:before{
      display: block;
      content: "";
      position: absolute;
      left:10px;
      width:1px;
      height:20px;
      background:#ccc;
    }

    &:after{
      top:-20px;
    }

    &:before{
      top:20px;
    }
  }
}
</style>

